﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Hangjing.DBUtility;
using Hangjing.Model;

namespace Hangjing.SQLServerDAL
{
    /// <summary>
    /// 配送员工资记录
    /// </summary>
    public partial class DeliverSalaryLog
    {
        /// <summary>
        /// 统计
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DeliverInfo GetSum(string where)
        {
            string sql = "select SUM(sendmoney) AS shopdiscountmoney,SUM(revefloat1)as s_salary,SUM(sendmoney-revefloat1)as s_deliverprofit ";


            sql += " , sum (case when reveint2 = 100 then(1) else 0   end)  as fulltimedeliver";
            sql += " , sum (case when reveint2 = 90 then(1) else 0   end)  as parttimedeliver";

            sql += " from DeliverSalaryLog  where " + where;

            DeliverInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, null))
            {
                if (dr.Read())
                {
                    model = new DeliverInfo();

                    model.salary = HJConvert.ToDecimal(dr["s_salary"]);
                    model.deliverprofit = HJConvert.ToDecimal(dr["s_deliverprofit"]);
                    model.shopdiscountmoney = HJConvert.ToDecimal(dr["shopdiscountmoney"]);

                    model.UserName = HJConvert.ToString(dr["fulltimedeliver"]);
                    model.Password = HJConvert.ToString(dr["parttimedeliver"]);

                }
            }
            return model;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DeliverSalaryLogInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DeliverSalaryLog(");
            strSql.Append("deliverid,addtime,sendmoney,basesalary,percentagerat,paysalary,paypercentage,admin,starttime,endtime,reveint1,reveint2,reveint3,revevar1,revevar2,revevar3,revefloat1,revefloat2");
            strSql.Append(") values (");
            strSql.Append("@deliverid,@addtime,@sendmoney,@basesalary,@percentagerat,@paysalary,@paypercentage,@admin,@starttime,@endtime,@reveint1,@reveint2,@reveint3,@revevar1,@revevar2,@revevar3,@revefloat1,@revefloat2");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@deliverid", SqlDbType.Int,4) ,
                new SqlParameter("@addtime", SqlDbType.DateTime) ,
                new SqlParameter("@sendmoney", SqlDbType.Decimal,5) ,
                new SqlParameter("@basesalary", SqlDbType.Int,4) ,
                new SqlParameter("@percentagerat", SqlDbType.Int,4) ,
                new SqlParameter("@paysalary", SqlDbType.Int,4) ,
                new SqlParameter("@paypercentage", SqlDbType.Decimal,5) ,
                new SqlParameter("@admin", SqlDbType.VarChar,256) ,
                new SqlParameter("@starttime", SqlDbType.DateTime) ,
                new SqlParameter("@endtime", SqlDbType.VarChar,4000) ,
                new SqlParameter("@reveint1", SqlDbType.Int,4) ,
                new SqlParameter("@reveint2", SqlDbType.Int,4) ,
                new SqlParameter("@reveint3", SqlDbType.Int,4) ,
                new SqlParameter("@revevar1", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar2", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar3", SqlDbType.VarChar,256) ,
                new SqlParameter("@revefloat1", SqlDbType.Decimal,5) ,
                new SqlParameter("@revefloat2", SqlDbType.Decimal,5)

            };

            parameters[0].Value = model.deliverid;
            parameters[1].Value = model.addtime;
            parameters[2].Value = model.sendmoney;
            parameters[3].Value = model.basesalary;
            parameters[4].Value = model.percentagerat;
            parameters[5].Value = model.paysalary;
            parameters[6].Value = model.paypercentage;
            parameters[7].Value = model.admin;
            parameters[8].Value = model.starttime;
            parameters[9].Value = model.endtime;
            parameters[10].Value = model.reveint1;
            parameters[11].Value = model.reveint2;
            parameters[12].Value = model.reveint3;
            parameters[13].Value = model.revevar1;
            parameters[14].Value = model.revevar2;
            parameters[15].Value = model.revevar3;
            parameters[16].Value = model.revefloat1;
            parameters[17].Value = model.revefloat2;
            return HJConvert.ToInt32(SQLHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters));
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(DeliverSalaryLogInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DeliverSalaryLog set ");
            strSql.Append(" deliverid = @deliverid , ");
            strSql.Append(" addtime = @addtime , ");
            strSql.Append(" sendmoney = @sendmoney , ");
            strSql.Append(" basesalary = @basesalary , ");
            strSql.Append(" percentagerat = @percentagerat , ");
            strSql.Append(" paysalary = @paysalary , ");
            strSql.Append(" paypercentage = @paypercentage , ");
            strSql.Append(" admin = @admin , ");
            strSql.Append(" starttime = @starttime , ");
            strSql.Append(" endtime = @endtime , ");
            strSql.Append(" reveint1 = @reveint1 , ");
            strSql.Append(" reveint2 = @reveint2 , ");
            strSql.Append(" reveint3 = @reveint3 , ");
            strSql.Append(" revevar1 = @revevar1 , ");
            strSql.Append(" revevar2 = @revevar2 , ");
            strSql.Append(" revevar3 = @revevar3 , ");
            strSql.Append(" revefloat1 = @revefloat1 , ");
            strSql.Append(" revefloat2 = @revefloat2  ");
            strSql.Append(" where sid=@sid ");

            SqlParameter[] parameters =
            {
                new SqlParameter("@sid", SqlDbType.Int,4) ,
                new SqlParameter("@deliverid", SqlDbType.Int,4) ,
                new SqlParameter("@addtime", SqlDbType.DateTime) ,
                new SqlParameter("@sendmoney", SqlDbType.Decimal,5) ,
                new SqlParameter("@basesalary", SqlDbType.Int,4) ,
                new SqlParameter("@percentagerat", SqlDbType.Int,4) ,
                new SqlParameter("@paysalary", SqlDbType.Int,4) ,
                new SqlParameter("@paypercentage", SqlDbType.Decimal,5) ,
                new SqlParameter("@admin", SqlDbType.VarChar,256) ,
                new SqlParameter("@starttime", SqlDbType.DateTime) ,
                new SqlParameter("@endtime", SqlDbType.VarChar,4000) ,
                new SqlParameter("@reveint1", SqlDbType.Int,4) ,
                new SqlParameter("@reveint2", SqlDbType.Int,4) ,
                new SqlParameter("@reveint3", SqlDbType.Int,4) ,
                new SqlParameter("@revevar1", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar2", SqlDbType.VarChar,256) ,
                new SqlParameter("@revevar3", SqlDbType.VarChar,256) ,
                new SqlParameter("@revefloat1", SqlDbType.Decimal,5) ,
                new SqlParameter("@revefloat2", SqlDbType.Decimal,5)

            };

            parameters[0].Value = model.sid;
            parameters[1].Value = model.deliverid;
            parameters[2].Value = model.addtime;
            parameters[3].Value = model.sendmoney;
            parameters[4].Value = model.basesalary;
            parameters[5].Value = model.percentagerat;
            parameters[6].Value = model.paysalary;
            parameters[7].Value = model.paypercentage;
            parameters[8].Value = model.admin;
            parameters[9].Value = model.starttime;
            parameters[10].Value = model.endtime;
            parameters[11].Value = model.reveint1;
            parameters[12].Value = model.reveint2;
            parameters[13].Value = model.reveint3;
            parameters[14].Value = model.revevar1;
            parameters[15].Value = model.revevar2;
            parameters[16].Value = model.revevar3;
            parameters[17].Value = model.revefloat1;
            parameters[18].Value = model.revefloat2;
            return SQLHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }


        /// <summary>
        /// 获取记录所有字段
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <param>sid</param>
        /// <returns>DeliverSalaryLogInfo</returns>
        public DeliverSalaryLogInfo GetModel(int sid)
        {
            string sql = "select sid,deliverid,addtime,sendmoney,basesalary,percentagerat,paysalary,paypercentage,admin,starttime,endtime,reveint1,reveint2,reveint3,revevar1,revevar2,revevar3,revefloat1,revefloat2 from DeliverSalaryLog where  sid = @sid";
            SqlParameter parameter = new SqlParameter("@sid", SqlDbType.Int, 4);
            parameter.Value = sid;
            DeliverSalaryLogInfo model = null;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.Text, sql, parameter))
            {
                if (dr.Read())
                {
                    model = new DeliverSalaryLogInfo();
                    model.sid = HJConvert.ToInt32(dr["sid"]);
                    model.deliverid = HJConvert.ToInt32(dr["deliverid"]);
                    model.addtime = HJConvert.ToDateTime(dr["addtime"]);
                    model.sendmoney = HJConvert.ToDecimal(dr["sendmoney"]);
                    model.basesalary = HJConvert.ToInt32(dr["basesalary"]);
                    model.percentagerat = HJConvert.ToInt32(dr["percentagerat"]);
                    model.paysalary = HJConvert.ToInt32(dr["paysalary"]);
                    model.paypercentage = HJConvert.ToDecimal(dr["paypercentage"]);
                    model.admin = HJConvert.ToString(dr["admin"]);
                    model.starttime = HJConvert.ToDateTime(dr["starttime"]);
                    model.endtime = HJConvert.ToString(dr["endtime"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);
                    model.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    model.reveint3 = HJConvert.ToInt32(dr["reveint3"]);
                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revefloat1 = HJConvert.ToDecimal(dr["revefloat1"]);
                    model.revefloat2 = HJConvert.ToDecimal(dr["revefloat2"]);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得总记录数
        /// </summary>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int GetCount(string strWhere)
        {
            return (int)SQLHelper.ExecuteScalar(CommandType.StoredProcedure, "pagecount", new SqlParameter("@tblName", "DeliverSalaryLog"), new SqlParameter("@strWhere", strWhere));
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pagesize">页尺寸</param>
        /// <param name="pageindex">页索引</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="orderName">排序字段</param>
        /// <param name="orderType">排序类型，1为降序，0为升序</param>
        /// <returns>列表</returns>
        ///此代码由杭景科技代码内部生成器自动生成
        public IList<DeliverSalaryLogInfo> GetList(int pagesize, int pageindex, string strWhere, string orderName, int orderType)
        {
            IList<DeliverSalaryLogInfo> infos = new List<DeliverSalaryLogInfo>();
            SqlParameter[] parameters =
                    {
                new SqlParameter("@tblName", SqlDbType.VarChar,255),
                new SqlParameter("@strGetFields", SqlDbType.VarChar,1000),
                new SqlParameter("@primary", SqlDbType.VarChar,255),
                new SqlParameter("@orderName", SqlDbType.VarChar,255),
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@OrderType", SqlDbType.Bit),
                new SqlParameter("@strWhere", SqlDbType.VarChar,1500)
            };
            parameters[0].Value = "DeliverSalaryLog";
            parameters[1].Value = "sid,deliverid,addtime,sendmoney,basesalary,percentagerat,paysalary,paypercentage,admin,starttime,endtime,reveint1,reveint2,reveint3,revevar1,revevar2,revevar3,revefloat1,revefloat2";
            parameters[2].Value = "sid";
            parameters[3].Value = orderName;
            parameters[4].Value = pagesize;
            parameters[5].Value = pageindex;
            parameters[6].Value = orderType;
            parameters[7].Value = strWhere;
            using (SqlDataReader dr = SQLHelper.ExecuteReader(CommandType.StoredProcedure, "pageselectpri", parameters))
            {
                while (dr.Read())
                {
                    DeliverSalaryLogInfo model = new DeliverSalaryLogInfo();
                    model.sid = HJConvert.ToInt32(dr["sid"]);
                    model.deliverid = HJConvert.ToInt32(dr["deliverid"]);
                    model.addtime = HJConvert.ToDateTime(dr["addtime"]);
                    model.sendmoney = HJConvert.ToDecimal(dr["sendmoney"]);
                    model.basesalary = HJConvert.ToInt32(dr["basesalary"]);
                    model.percentagerat = HJConvert.ToInt32(dr["percentagerat"]);
                    model.paysalary = HJConvert.ToInt32(dr["paysalary"]);
                    model.paypercentage = HJConvert.ToDecimal(dr["paypercentage"]);
                    model.admin = HJConvert.ToString(dr["admin"]);
                    model.starttime = HJConvert.ToDateTime(dr["starttime"]);
                    model.endtime = HJConvert.ToString(dr["endtime"]);
                    model.reveint1 = HJConvert.ToInt32(dr["reveint1"]);
                    model.reveint2 = HJConvert.ToInt32(dr["reveint2"]);
                    model.reveint3 = HJConvert.ToInt32(dr["reveint3"]);
                    model.revevar1 = HJConvert.ToString(dr["revevar1"]);
                    model.revevar2 = HJConvert.ToString(dr["revevar2"]);
                    model.revevar3 = HJConvert.ToString(dr["revevar3"]);
                    model.revefloat1 = HJConvert.ToDecimal(dr["revefloat1"]);
                    model.revefloat2 = HJConvert.ToDecimal(dr["revefloat2"]);
                    infos.Add(model);
                }
            }
            return infos;
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="Id"></param>
        /// 此代码由杭景科技代码内部生成器自动生成
        /// <returns></returns>
        public int DelDeliverSalaryLog(int Id)
        {
            StringBuilder str = new StringBuilder();
            str.Append("delete from DeliverSalaryLog where sid=@sid");
            SqlParameter[] Para =
                {
                new SqlParameter("@sid",SqlDbType.Int)
            };
            Para[0].Value = Id;

            return SQLHelper.ExecuteNonQuery(CommandType.Text, str.ToString(), Para);
        }


        /// <summary>
        /// 批量删除记录
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        /// 此代码由杭景科技代码内部生成器自动生成
        public int DelList(string IdList)
        {
            StringBuilder str = new StringBuilder();
            str.Append("delete from DeliverSalaryLog where sid in ({0})");

            return SQLHelper.ExecuteNonQuery(CommandType.Text, string.Format(str.ToString(), IdList), null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="starttime"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public int payoff(string starttime, string admin)
        {
            SqlParameter[] Para =
            {
                new SqlParameter("@starttime",SqlDbType.VarChar,50),
                new SqlParameter("@admin",SqlDbType.VarChar,50)
            };
            Para[0].Value = starttime;
            Para[1].Value = admin;

            SQLHelper.ExecuteNonQuery(CommandType.StoredProcedure, "DeliverSalaryLog_payoff", Para); ;

            return 1;
        }

    }
}

